package cn.edu.scau.dao;

import cn.edu.scau.entity.Customer;

import java.util.List;

import org.apache.ibatis.annotations.CacheNamespace;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.type.JdbcType;

@Mapper
@CacheNamespace
public interface CustomerMapper {
    @Delete({
        "delete from customer",
        "where customerID = #{customerid,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer customerid);

    @Insert({
        "insert into customer (customerID, customerName, ",
        "customerSex, customerAge, ",
        "customerTelephone, customerUseflag)",
        "values (#{customerid,jdbcType=INTEGER}, #{customername,jdbcType=VARCHAR}, ",
        "#{customersex,jdbcType=VARCHAR}, #{customerage,jdbcType=INTEGER}, ",
        "#{customertelephone,jdbcType=VARCHAR}, #{customeruseflag,jdbcType=BIT})"
    })
    @Options(useGeneratedKeys = true,keyColumn = "customerID",keyProperty = "customerid")
    int insert(Customer record);

    @Options(useGeneratedKeys = true,keyColumn = "customerID",keyProperty = "customerid")
    @InsertProvider(type=CustomerSqlProvider.class, method="insertSelective")
    int insertSelective(Customer record);

    @Select({
        "select",
        "customerID, customerName, customerSex, customerAge, customerTelephone, customerUseflag",
        "from customer",
        "where customerID = #{customerid,jdbcType=INTEGER}",
        "and customerUseflag = true"
    })
    @Results({
        @Result(column="customerID", property="customerid", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="customerName", property="customername", jdbcType=JdbcType.VARCHAR),
        @Result(column="customerSex", property="customersex", jdbcType=JdbcType.VARCHAR),
        @Result(column="customerAge", property="customerage", jdbcType=JdbcType.INTEGER),
        @Result(column="customerTelephone", property="customertelephone", jdbcType=JdbcType.VARCHAR),
        @Result(column="customerUseflag", property="customeruseflag", jdbcType=JdbcType.BIT)
    })
    Customer selectByPrimaryKey(Integer customerid);
    
    @Select({
        "select",
        "customerID, customerName, customerSex, customerAge, customerTelephone, customerUseflag",
        "from customer",
        "where customerUseflag = true"
    })
    List<Customer> selectAll();
    
    
    @SelectProvider(type = CustomerSqlProvider.class,method = "findByIdName")
    List<Customer> findByIdName(Integer customerID,String customerName);
    
    @UpdateProvider(type=CustomerSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(Customer record);

    @Update({
        "update customer",
        "set customerName = #{customername,jdbcType=VARCHAR},",
          "customerSex = #{customersex,jdbcType=VARCHAR},",
          "customerAge = #{customerage,jdbcType=INTEGER},",
          "customerTelephone = #{customertelephone,jdbcType=VARCHAR},",
          "customerUseflag = #{customeruseflag,jdbcType=BIT}",
        "where customerID = #{customerid,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(Customer record);
}